package com.aliyun.heiyu.powermaster.server.service;

import com.aliyun.heiyu.common.util.DateUtil;
import com.aliyun.heiyu.powermaster.server.dao.DeviceCommonDao;
import com.aliyun.heiyu.powermaster.server.dao.DictionaryConfigDao;
import com.aliyun.heiyu.powermaster.server.dao.StateGridCollectInfoDao;
import com.aliyun.heiyu.powermaster.server.dao.entity.DeviceIPDeviceIdDTO;
import com.aliyun.heiyu.powermaster.server.dao.entity.StateGridPowerctlAgentServerPerformanceDataPO;
import com.aliyun.heiyu.powermaster.server.dao.impl.DeviceCommonImpl;
import com.aliyun.heiyu.powermaster.server.dao.impl.DictionaryConfigDaoImpl;
import com.aliyun.heiyu.powermaster.server.dao.impl.StateGridCollectInfoImpl;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import com.aliyun.heiyu.common.util.HttpUtil;
import com.aliyun.heiyu.powermaster.server.dao.entity.StateGridPowerctlAgentServerPerformancePO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class StateGridPowerctlAgentServerPerformanceServiceImpl implements Runnable {
    private static Logger logger = LoggerFactory.getLogger(StateGridPowerctlAgentServerPerformanceServiceImpl.class);

    private static DeviceCommonDao deviceCommonDao = new DeviceCommonImpl();
    private static StateGridCollectInfoDao stateGridCollectInfoDao = new StateGridCollectInfoImpl();
    private static DictionaryConfigDao dictionaryConfigDao = new DictionaryConfigDaoImpl();

    private String deviceSwitch;
    private DeviceIPDeviceIdDTO deviceIPDeviceId;

    StateGridPowerctlAgentServerPerformanceServiceImpl() {

    }

    StateGridPowerctlAgentServerPerformanceServiceImpl(String deviceSwitch, DeviceIPDeviceIdDTO deviceIPDeviceId) {
        this.deviceIPDeviceId = deviceIPDeviceId;
        this.deviceSwitch = deviceSwitch;
    }

    public String getDeviceSwitch() {
        return deviceSwitch;
    }

    public void setDeviceSwitch(String deviceSwitch) {
        this.deviceSwitch = deviceSwitch;
    }

    public DeviceIPDeviceIdDTO getDeviceIPDeviceId() {
        return deviceIPDeviceId;
    }

    public void setDeviceIPDeviceId(DeviceIPDeviceIdDTO deviceIPDeviceId) {
        this.deviceIPDeviceId = deviceIPDeviceId;
    }



    public void getStateGridPowerctlAgentServerPerformance(String deviceSwitch, DeviceIPDeviceIdDTO deviceIPDeviceId,String port) throws Exception {
        logger.info("进入【getStateGridPowerctlAgentServerPerformance】");

        if (StringUtils.isNotBlank(deviceIPDeviceId.getIp())) {

            //https请求
            String urls = "https://" + deviceIPDeviceId.getIp() + ":"+port+"/stateGrid/powerctl_agent/server_performance?serverInfo=io_net_memory";
            if (StringUtils.isBlank(urls)) {
                return;
            }

            JSONObject json = null;
            try {
                json = HttpUtil.getRequest(urls);
                logger.info("getStateGridPowerctlAgentServerPerformance-http:【{}】,返回参数【{}】", urls, json);
            } catch (Exception e) {
                logger.info("getStateGridPowerctlAgentServerPerformance-Exception:【{}】,返回参数【{}】", e, json);
            }
            if (null == json) {
                logger.info("getStateGridPowerctlAgentServerPerformance-Exception采集json为空:【{}】", json);
                return;
            }
            StateGridPowerctlAgentServerPerformancePO enpd = com.alibaba.fastjson.JSONObject.parseObject(json.toString(), StateGridPowerctlAgentServerPerformancePO.class);
            Map<String, String> map = new HashMap<>();

            StateGridPowerctlAgentServerPerformanceDataPO data = enpd.getData();
            String time = DateUtil.millisecondToDate(System.currentTimeMillis());

            //open 关 x86 不走这个
            if (deviceSwitch.equals("open") || (StringUtils.isNotBlank(deviceIPDeviceId.getServer_type()) && deviceIPDeviceId.getServer_type().equals("arm"))) {
                map.put("gmt_create", time);
                map.put("gmt_modified", time);
                map.put("collect_time", time);
                map.put("frame_power", data.getFrame_power());
                map.put("server_power", data.getServer_power());
                map.put("cpu_usage", data.getCpu_use());
                map.put("cpu_freq", data.getCpu_freq_avg());
                map.put("cpu_core_num", data.getCpu_core_num());//cpu核数
                map.put("iowait", data.getIowait());
                map.put("total_memory", data.getTotal_memory());
                map.put("available_memory", data.getAvailable_memory());
                map.put("nvme_temp_max", data.getNetwork_read());
                map.put("network_write", data.getNetwork_write());
                map.put("fan_rpm", data.getFan_rpm());
                map.put("inlet_temp", data.getInlet_temp());
                map.put("outlet_temp", data.getOutlet_temp());
                if (null != data.getPch_temp()) {
                    String pch_temp = data.getPch_temp();
                    if (pch_temp.contains(",")) {
                        pch_temp = pch_temp.substring(0, pch_temp.indexOf(","));
                    }
                    map.put("pch_temp", pch_temp);
                }
                map.put("cpu_temp", data.getCpu_temp());
                map.put("cpu_margin_temp", data.getCpu_margin_temp());
                map.put("dimmg_temp", data.getDimmg_temp());
                map.put("cpu_vr_temp", data.getCpu_vr_temp());
                map.put("cpu_voltage", data.getCpu_voltage());
                map.put("ps_power", data.getPs_power());
                map.put("nvme_temp", data.getNvm_temp());
                map.put("ps_status", data.getPs_status());
                map.put("server_sn", deviceIPDeviceId.getDevice_id());
                map.put("architecture", data.getArchitecture());
                try {
                    stateGridCollectInfoDao.replaceInsert(map, "state_grid_collect_info");
                } catch (SQLException e) {
                    logger.error("getStateGridPowerctlAgentServerPerformancearm获取数据失败【{}】", e);
                }
            }
        }
        return;
    }


    public static void main(String[] args) throws Exception {
        StateGridPowerctlAgentServerPerformanceServiceImpl hh=new StateGridPowerctlAgentServerPerformanceServiceImpl();
    }
    @Override
    public void run() {
        try {
            getStateGridPowerctlAgentServerPerformance(deviceSwitch,deviceIPDeviceId,"32341");
            getStateGridPowerctlAgentServerPerformance(deviceSwitch,deviceIPDeviceId,"32342");
            getStateGridPowerctlAgentServerPerformance(deviceSwitch,deviceIPDeviceId,"32343");
            getStateGridPowerctlAgentServerPerformance(deviceSwitch,deviceIPDeviceId,"32344");
            getStateGridPowerctlAgentServerPerformance(deviceSwitch,deviceIPDeviceId,"32345");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
